package com.fjycnet.webview;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.webkit.WebView;

import com.orangelife.R;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

/**
 * Created by ZeroVoid on 2018-7-12.
 */

public class WvActivity extends AppCompatActivity {

    private WebView wv1;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_wv);
        wv1 = findViewById(R.id.wv1);
        String string = "<style>img{max-width:100%}</style><p><img src=\"http://p3.pstatp.com/large/pgc-image/153081390601264eebe6ee1\" img_width=\"1080\" img_height=\"720\"</p><p><img src=\"http://p9.pstatp.com/large/pgc-image/153081390632703d8f747ad\" img_width=\"640\" img_height=\"427\"/><img src=\"http://p3.pstatp.com/large/pgc-image/1530813906484932d9e09fc\" img_width=\"640\" img_height=\"427\" ></p></body>";
//        string = getNewContent(string);
        wv1.loadData(string, "text/html", "UTF-8");
    }

    public static String getNewContent(String htmltext) {
        try {
            Document doc = Jsoup.parse(htmltext);
            Elements elements = doc.getElementsByTag("img");
            for (Element element : elements) {
//                doc.select("div.comments a").attr("rel", "nofollow");
                element.attr("width", "100%").attr("height", "auto");
            }

            return doc.toString();
        } catch (Exception e) {
            return htmltext;
        }
    }
}
